#include "foodfinder.h"

#include "QDebug"


bool isToday(QString dayString);
bool isAnHeader(QWebElement& webelem);

FoodFinder::FoodFinder(QWebView*  webView)

{

    this->webView=webView;
    webView->load(QUrl("http://www.puv.fi/amica/serveri/"));
    connect(webView,SIGNAL(loadFinished(bool)),this,SLOT(pageStart(bool)));
}

//return foodlist

QList< QPair<QString,float> >* FoodFinder::getFoodList()
{
    return _foodList;
}
FoodFinder::~FoodFinder()
{

}
QWebElement FoodFinder::document() const {
    return webView->page()->mainFrame()->documentElement();
}

void FoodFinder::pageStart(bool ok){

    _foodList = new  QList< QPair<QString,float> > ();
    if(!ok){
        emit done(false);
        return;
    }

    //the first table contains the food information
    QWebElement menutable =  document().findFirst("table tbody");
    QWebElementCollection dayrows= menutable.findAll("tr");


    //we first look for the row that contains today's menu
    QWebElement todayHeader;
    foreach(QWebElement elem, dayrows ){

        if(isAnHeader(elem) && isToday(elem.toPlainText())){

            todayHeader=elem;
            break;
        }
    }

    if(todayHeader.isNull()){
        qDebug()<<"No food today header null";
        emit done(false);
        return ;
    }

    else {
        //we found today's menu
        /*The table contains tr and the food offered are of varied lenght
        so, we keep checking the tr's until we come across an header (th tag)
        */


        QWebElement next = todayHeader.nextSibling();
        while(!isAnHeader(next)){
            QWebElementCollection foodElem= next.findAll("td");
            QWebElement englishNameElem =foodElem.at(2);
            QWebElement stuPrice = foodElem.at(4);

            QString engName = englishNameElem.toPlainText();
            // convert the string (2,50) to the float 2.5
            QString ps = stuPrice.toPlainText().replace(',','.').replace('(',' ').replace(')',' ').trimmed();
            float price = ps.toFloat();

            if(!engName.isEmpty()){

                QPair<QString,float> pair (engName,price);
                _foodList->append(pair);
            }
            next= next.nextSibling();
        }

    }

//    qDebug()<<"done with food";

    emit done(true);



}


/**
  Checks if this webelem is an th rather than a tr
  */
bool isAnHeader(QWebElement& webelem){
    QWebElementCollection ths= webelem.findAll("th");
    return ths.count()>0;

}


/**
  We want to campare a string like  Ti/Ti/Tue 8.3.
  to the date today.
  */
bool isToday(QString dayString){



    QStringList list= dayString.split("/",QString::KeepEmptyParts);
    QString englishDate = list[2];

    QString tday,month;


    tday.setNum ( QDate::currentDate ().day ());
    month.setNum ( QDate::currentDate ().month ());

    QString c =tday+"."+month+".";

    //check based on the last 4 characters i.e 8.3. forexample
    if(dayString.contains (c)){
        return true;
    }
    //incase the delimeter . was change , check based on day i.e Mon, Tue
    QString day =  QDate::shortDayName( QDate::currentDate ().dayOfWeek());
    if(englishDate.contains(day)){
        return true;
    }
    return false;

}
